<?
session_start();

/////////////////////////////////////////////////////////////////////////////////////////////////	
function get_conn($dbase){

					
	if($_SERVER['SERVER_NAME'] =="localhost"){
		$server = "localhost";
		$user ="root";
		$pwd = "";
	}
	

	
	if($db ==''){
		@ $db=mysql_connect ($server, $user, $pwd);				
		if (!$db){
			error_handling("get_conn: ".$sql,mysql_error());
		}			
		mysql_select_db ($dbase); 
	
	}	
	return $db;
}		
//////////////////////////////////////////////////////////
function get_user_ftp(){
		return "floorrep@floorreports.com";				
}
//////////////////////////////////////////////////////////
function get_pwd_ftp(){
		return "n1c0r3tt3";			
}
//////////////////////////////////////////////////////////
function get_ftp_domain(){
	return "66.152.167.179";			
}
//////////////////////////////////////////////////////////
function get_ftp_path(){
	if($_SERVER['SERVER_NAME'] =="www.floorreports.com"){
		return "";			
	}		
}
//////////////////////////////////////////////////////////////////////////////////////////////////	
function getStatus($value,$param){
	if ($value == $param)
		return "selected";
	else	
		return "";
}


//////////////////////////////////////////////////////////////////////////////////////////////////	
function dbcombo($value,$descripcion,$tabla,$nombre,$seleccionado,$order,$javascript,$extraitems){

		$sql= "select ".$value." as myvalue , ".$descripcion." as mydesc from ".$tabla." order by ".$order;
		$result = mysql_query($sql);
		$combo= " <select name='".$nombre."' ".$javascript." id='".$nombre."'>";
		if($result){
			if(mysql_num_rows($result)>0){
				$data = $extraitems;
				$num_results = mysql_num_rows($result);
				  for ($i=0; $i <$num_results; $i++){		 
					$row = mysql_fetch_array($result);
					$selected="";
					$selected = getStatus($row["myvalue"],$seleccionado);										
					$data.= " <option value='".$row["myvalue"]."'".$selected." >".$row["mydesc"]."</option>";
				  }//for
			}else{
				$data.= " <option value='0'>Not Available</option>";
			}
		}//if
		else{
			error_handling("dbcombo: ".$sql,mysql_error());
		}
		$combo.= $data."</select>";
		 if($data==""){
		 	$combo = $sql;
		 }
		 return $combo;
}//function 
	
//////////////////////////////////////////////////////////////////////////////////////////////////	
/*
select id as myvalue, desc as mydesc from ....
*/
function dbcombosql($sqlquery,$nombre,$seleccionado,$javascript,$extraitems){
		$result = mysql_query($sqlquery);
		$combo= " <select name='".$nombre."'".$javascript."> ";
		$data = $extraitems;
		if($result){
			$num_results = mysql_num_rows($result);
			if($num_results>0){
			  for ($i=0; $i <$num_results; $i++){		 
				$row = mysql_fetch_array($result);
				$selected="";
				$selected = getStatus($row["myvalue"],$seleccionado);										
				$data.= " <option value='".$row["myvalue"]."'".$selected." >".$row["mydesc"]."</option>";
			  }//for
			}else{
				$data.= " <option value='0'>Not Available&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>";
			}
		}//if
		$combo.= $data."</select>";
		 if($data==""){
		 	$combo = $sqlquery;
		 }
		 return $combo;
		}//function 
		


//////////////////////////////////////////////////////////
function format_date($date){
	if($date =='N/A'){
		return 'N/A';
	}
	$date_array = explode('-',$date);
	$new_date_a=$date_array[0];
	$new_date_m=$date_array[1];
	$new_date_d=$date_array[2];	
	
	if(abs($new_date_d)<10){
		$new_date_d = abs($new_date_d);
	}

	if(abs($new_date_m)<10){
		$new_date_m = abs($new_date_m);
	}	
	
	$new_date_a = substr($new_date_a,2,4);	
	
	$new_date = $new_date_m."-".$new_date_d."-".$new_date_a;
	return $new_date;
}
//////////////////////////////////////////////////////////
function un_format_date($date){
	$date_array = explode('-',$date);
	//07/24/2006
	
	$new_date_a= $date_array[2];
	$new_date_m=$date_array[0];
	$new_date_d=$date_array[1];	

	$new_date = $new_date_a."-".$new_date_m."-".$new_date_d;
	return $new_date;
}
//////////////////////////////////////////////////////////

function format_text($text){
 return "<div align='justify'>".str_replace("\n","<br/>",$text)."</div>";
}
//////////////////////////////////////////////////////////
function format($precio){
	$partes = explode(".",$precio);
	$precio = $partes[0];
	$decimales =$partes[1];
	
	if($precio =='0.00'){
		return "$0.00";
	}
	if($decimales ==''){
		$decimales="00";
	}
		
	if(strlen($precio)>6){
		$p3 = substr($precio,strlen($precio)-3,3);
		$p2 = substr($precio,strlen($precio)-6,3);
		$p1 = substr($precio,0,strlen($precio)-6);	
		$formated = "$".$p1.",".$p2.",".$p3;
	}
	else{
		if(strlen($precio)>3){
			$p3 = substr($precio,strlen($precio)-3,3);
			$p2 = substr($precio,0,strlen($precio)-3);	
			$formated = "$".$p2.",".$p3;	
		}else{
			if($precio != 0){
				$formated = "$".$precio;
			}else{
				$formated ="N/A";
			}	
		}
	}
	if($formated !='N/A'){
		$formated .=".".$decimales;
	}			
	
	return $formated;

}
/////////////////////////////////////////////////////////

function get_last($table,$key,$where){

	$sql ="select max(".$key.") as current from ".$table." where 1 ";
	if($where!=''){
		$sql .= " and ".$where;
	}
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		return $row['current'];
	}else{
		error_handling("get_last: ".$sql,mysql_error());
	}
}
 //////////////////////////////////////////////////////////////////////////////////////////////////
function getNext($tabla,$columsearch,$filtrocampo,$filtrovalue){
		$sqlquery= "select max(".$columsearch.")+1 as next from " .$tabla;
		if($filtrocampo!=''){
			$sqlwhere = " where ".$filtrocampo." = ".$filtrovalue;
		}else{
			$sqlwhere ="";
		}
		$sql = $sqlquery.$sqlwhere;		
		$result = mysql_query($sql);
		$text = "";
		if($result){
			$row = mysql_fetch_array($result);
			$next= $row["next"];
			if($next == NULL){
				return 1;
			}else{
				return $next;
			}
		}else{
			echo $sqlquery." ".mysql_error(); exit;
		}
}


///////////////////////////////////////////////////////////////////////////////////////////
function isnumeric($type){
	if (  ($type =='decimal') || 
		  ($type =='tinyint') || 
		  ($type =='smallint') || 
		  ($type =='mediumint') || 
		  ($type =='int')|| 
		  ($type =='bigint') || 
		  ($type =='float') || 
		  ($type =='double')
	   )
	   {
	   	return true;
	   }
	else{
		return false;
	}
}


function error_handling($sql,$error){

	foreach($_REQUEST as $key=>$value){
     $post_vars .= $key." =".htmlspecialchars($value)."\\r";
    }
	
	foreach($_SESSION as $key=>$value){
     $sess_vars .= $key." =".htmlspecialchars($value)."\\r";
    }	



	$body=  "Error ".$error." While Executing: <br><br> in Function: ".$sql." <br><br>at ".date("l dS of F Y h:i:s A").
	"<br><br>Error in File: <br>".$_SERVER['PHP_SELF'].
	"<br><br>Request Vars<br>".$post_vars.
	"<br><br>Session Vars<br><br>".$sess_vars;



///////////////////////////////////////////////////////////	new db implementation
	$id_user = $_SESSION['session_id_user'];
	if($id_user==''){
		$id_user = -1;
	}
	$time_stamp=date('Y-m-d H:i:s');
	$query =htmlspecialchars($sql);
	$browser_type= "";
	$browser_version = "";
	$error = htmlspecialchars($error);
	$page = $_SERVER['PHP_SELF'];
	$ip =$_SERVER['REMOTE_ADDR'];	
	$db = get_conn();
///////////////////////////////////////////////////////////////////
	$to = "dontpanic@danramza.com";
	$head = "Error report from ".$_SERVER['SERVER_NAME'];
	$headers .= "From: Floorreports.com <mailer@Floorreports.com>\n";
	$headers .= "X-Sender: <replys@questinspect.com>\n";
	$headers .= "X-Mailer: PHP\n"; //mailer
	$headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
	$headers .= "Return-Path: <replys@questinspect.com>>\n";
	$headers .= "Content-Type: text/html; charset=iso-8859-1\n";


	if($_SERVER['SERVER_NAME'] =="localhost" ){
		echo $body;
		exit;
	}


}
?>